package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IPublicationFeedBackSureView;
import com.cloudsigns.factorymanager2.engineering.adapter.GridAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.MediaBean;
import com.cloudsigns.factorymanager2.engineering.presenter.PublicationFeedBackSurePresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/6/17.
 */

public class FeedBackSurePublicationForManagerActivity extends Activity implements View.OnClickListener, IPublicationFeedBackSureView {
    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView tv_project_name, tv_engineering_name;
    private TextView edt_fankuiaccount, edt_feedback_time, edt_sureaccount, edt_suretime;
    private View rl_wentineirong, rl_wenti_img;
    private View rl_befor_publication, rl_after_publication;
    private View rl_sureaccount, rl_suretime;
    private GridView gridView_befor, gridView_after;
    private GridView gridView_wenti_img;
    private TextView edt__wentineirong;
    private TextView btn_confirm;
    private boolean isclickable;

    private GridAdapter gridAdapter_before, gridAdapter_after;
    private List<String> imgUrlList_before = new ArrayList<>();
    private List<String> imgUrlList_after = new ArrayList<>();
    private GridAdapter gridAdapter_wenti;
    private List<String> imgUrlList_wenti = new ArrayList<>();

    private String tag = "";
    private MediaBean mediaBean;

    private PublicationFeedBackSurePresenter publicationFeedBackSurePresenter;
    private String orderId, itemId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_feedbacksure_publication_for_manager);

        publicationFeedBackSurePresenter = new PublicationFeedBackSurePresenter(this);
        init();
        Intent mIntent = getIntent();
        if (null != mIntent) {
            tag = mIntent.getStringExtra(ConstantUtil.KEY_1);
            mediaBean = (MediaBean) mIntent.getSerializableExtra(ConstantUtil.KEY_2);

            if (null != mediaBean) {

                orderId = mediaBean.getOrderId();
                itemId = mediaBean.getId();

                tv_project_name.setText(mediaBean.getMediaName());
                tv_engineering_name.setText("/" + mediaBean.getNumber());

                edt__wentineirong.setText(mediaBean.getUnusualReason());
                edt_fankuiaccount.setText(mediaBean.getFeedbackAccountId());
                edt_feedback_time.setText(DateUtil.getYyyy_Mm_Dd(mediaBean.getFeedbackDate()));

                edt_sureaccount.setText(mediaBean.getHandlerAccountId());
                edt_suretime.setText(DateUtil.getYyyy_Mm_Dd(mediaBean.getHandleDate()));

                List<String> beforePicList = mediaBean.getBeforePicList();
                if (null != beforePicList) {
                    imgUrlList_before.clear();
                    imgUrlList_before.addAll(beforePicList);

                    gridAdapter_before.setData(imgUrlList_before);
                    gridAdapter_before.notifyDataSetChanged();
                }

                List<String> afterPicList = mediaBean.getAfterPicList();
                if (null != afterPicList) {
                    imgUrlList_after.clear();
                    imgUrlList_after.addAll(afterPicList);

                    gridAdapter_after.setData(imgUrlList_after);
                    gridAdapter_after.notifyDataSetChanged();
                }

                List<String> feedbackPicList = mediaBean.getFeedbackPicList();
                if (null != feedbackPicList) {
                    imgUrlList_wenti.clear();
                    imgUrlList_wenti.addAll(feedbackPicList);

                    gridAdapter_wenti.setData(imgUrlList_wenti);
                    gridAdapter_wenti.notifyDataSetChanged();

                }

                if (null == beforePicList || beforePicList.size() <= 0) {
                    rl_befor_publication.setVisibility(View.GONE);

                } else {
                    rl_befor_publication.setVisibility(View.VISIBLE);


                }

                if (null == afterPicList || afterPicList.size() <= 0) {
                    rl_after_publication.setVisibility(View.GONE);
                } else {
                    rl_after_publication.setVisibility(View.VISIBLE);
                }

                String unusualReason = mediaBean.getUnusualReason();
                if (null == unusualReason || "null".equals(unusualReason) || TextUtils.isEmpty(unusualReason)) {
                    rl_wentineirong.setVisibility(View.GONE);
                    rl_wenti_img.setVisibility(View.GONE);
                } else {
                    rl_wentineirong.setVisibility(View.VISIBLE);
                    rl_wenti_img.setVisibility(View.VISIBLE);
                }

                String handlerAccountId = mediaBean.getHandlerAccountId();
                if (null == handlerAccountId || "null".equals(handlerAccountId) || TextUtils.isEmpty(handlerAccountId)) {
                    rl_sureaccount.setVisibility(View.GONE);
                    rl_suretime.setVisibility(View.GONE);
                } else {
                    rl_sureaccount.setVisibility(View.VISIBLE);
                    rl_suretime.setVisibility(View.VISIBLE);
                }


                CommonEnum.MediaStatus mediaStatus = mediaBean.getStatus();
                if (mediaStatus == CommonEnum.MediaStatus.PROCESSING) {  //上刊前
                    btnClickable(false);

                } else if (mediaStatus == CommonEnum.MediaStatus.PAUSE) {
                    btnClickable(false);

                } else if (mediaStatus == CommonEnum.MediaStatus.PROBLEM_FEEDBACK) {
                    btnClickable(true);

                } else if (mediaStatus == CommonEnum.MediaStatus.PROBLEM_APPEAR) {
                    btnClickable(false);
                } else if (mediaStatus == CommonEnum.MediaStatus.COMPLETE_FEEDBACK) {

                    btnClickable(true);

                } else if (mediaStatus == CommonEnum.MediaStatus.COMPLETE) {
                    btnClickable(false);

                } else {
                    btnClickable(false);
                }


            }
        }

    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);


        tv_project_name = findViewById(R.id.tv_project_name);
        tv_engineering_name = findViewById(R.id.tv_engineering_name);

        edt__wentineirong = findViewById(R.id.edt__wentineirong);

        edt_fankuiaccount = findViewById(R.id.edt_fankuiaccount);
        edt_feedback_time = findViewById(R.id.edt_feedback_time);
        edt_sureaccount = findViewById(R.id.edt_sureaccount);

        edt_suretime = findViewById(R.id.edt_suretime);

        gridView_befor = findViewById(R.id.gridView_befor);
        gridView_after = findViewById(R.id.gridView_after);
        gridView_wenti_img = findViewById(R.id.gridView_wenti_img);

        rl_wentineirong = findViewById(R.id.rl_wentineirong);
        rl_wenti_img = findViewById(R.id.rl_wenti_img);

        rl_befor_publication = findViewById(R.id.rl_befor_publication);
        rl_after_publication = findViewById(R.id.rl_after_publication);

        rl_sureaccount = findViewById(R.id.rl_sureaccount);
        rl_suretime = findViewById(R.id.rl_suretime);

        btn_confirm = findViewById(R.id.btn_confirm);

        gridView_befor.setNumColumns(3);
        gridAdapter_before = new GridAdapter(this);
        gridAdapter_before.setData(imgUrlList_before);

        gridView_befor.setAdapter(gridAdapter_before);

        gridView_after.setNumColumns(3);
        gridAdapter_after = new GridAdapter(this);
        gridAdapter_after.setData(imgUrlList_after);

        gridView_after.setAdapter(gridAdapter_after);

        gridView_wenti_img.setNumColumns(3);
        gridAdapter_wenti = new GridAdapter(this);
        gridAdapter_wenti.setData(imgUrlList_wenti);

        gridView_wenti_img.setAdapter(gridAdapter_wenti);


        tv_title.setText("反馈信息");


        rl_back.setOnClickListener(this);
        btn_confirm.setOnClickListener(this);
    }

    private void btnClickable(boolean bool) {
        isclickable = bool;
        if (bool) {
            btn_confirm.setBackgroundResource(R.mipmap.btn_long_bg_green);
            btn_confirm.setTextColor(getResources().getColor(R.color.rgb_ffffffff));
        } else {

            btn_confirm.setBackgroundResource(R.mipmap.btn_bg_short_gray);
            btn_confirm.setTextColor(getResources().getColor(R.color.rgb_ffffffff));
        }
    }

    private void feedbackConfirm() {
        publicationFeedBackSurePresenter.publicationFeedBackSure();
    }


    @Override
    public void onClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;

            case R.id.btn_confirm:
                if (isclickable)
                    feedbackConfirm();
                break;


        }
    }

    @Override
    public String getitemId() {
        return itemId;
    }

    @Override
    public String getOrderId() {
        return orderId;
    }

    @Override
    public void publicationFeedBackSureSuccessed() {
        Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show();
        Intent mIntent = new Intent();
        setResult(RESULT_OK, mIntent);
        finish();
    }

    @Override
    public void publicationFeedBackSureFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
